home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / corewars / amain.c next >
Encoding:
C/C++ Source or Header  |  1989-02-14  |  2.7 KB  |  117 lines

  1. /* this module contains the main function, which settles open/close
  2.    file i/o as well as little trivial details like adding an extension
  3.    and stuff like that.  It has been debuged (except for BUG NOTES)
  4.    so it's safe to trust. There is a little bit of inefficiency, but
  5.    that's justified since I want a more easily readable program */
  6.  
  7. #include "assem.h"
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include <strings.h>
  11.  
  12. char    *outname(str)
  13. char    *str;
  14. /* accepts an input string and outputs a proper output file with ".e"
  15.    extension.  If already has .e, as an extension, produce an error. 
  16.    BUG NOTE: even if it's as innoculous as .eex, etc (as long as the
  17.              extension starts with .e) it will still produce an error
  18.    Otherwise, remove current extension
  19.    and add .e extension.
  20.    returns pointer to new output name                                       */
  21. {
  22.     char    *newstr;
  23.     char    *dot;        /* position of '.' */
  24.  
  25.     if (!(newstr =(char *) malloc( (unsigned) strlen(str) + 3)))
  26.     {
  27.         printf("not enough memory --- outname\n");
  28.         exit(1);
  29.     }
  30.  
  31.     strcpy(newstr,str);
  32.  
  33.     if (!(dot = rindex(newstr,'.')))
  34.         strcat(newstr,".e");    /* no extenstion */
  35.     else if (*(dot + 1) == 'e')    /* same extension as output? */
  36.     {
  37.         printf("wrong input file name: %s\n", newstr);
  38.         printf("try moving to non .e extension --- outname\n");
  39.         exit(1);
  40.     }
  41.     else                /* perform surgery */
  42.     {
  43.         (*(dot + 1)) = 'e';
  44.         (*(dot + 2)) = '\0';
  45.     }
  46.  
  47.     return newstr;
  48. }
  49.  
  50.  
  51. /* main -- Open input and output files, giving default names if
  52.            necessary.  Detects errors like not being able to open files
  53.        etc.
  54. */
  55. main(argc, argv)
  56. int    argc;
  57. char    *argv[];
  58. {
  59.     FILE    *f1,*f2;        /* input file, output file */
  60.     char    *outfile = "NONAME",    /* default output file */
  61.         flag = 0;        /* standard input */
  62.  
  63.     if (argc == 1)        /* no arguments */
  64.     {
  65.         flag = 1;    /* read from standard input */
  66.         argv[1] = outfile;
  67.         argc = 2;    /* one file */
  68.     }
  69.  
  70.     for (;argc > 1; argc--)
  71.     {
  72.         if (flag)
  73.             f1 = stdin;    /* set file to standard input */
  74.         else
  75.             /* open input file */
  76.             if (!(f1 = fopen(argv[argc - 1],"r")))
  77.             {
  78.                 printf("oops cannot open file %s",
  79.                     argv[argc - 1]);
  80.                 printf("\n-- in main\n");
  81.                 exit(1);    /* error status 1 */
  82.             }
  83.  
  84.             /* open output file */
  85.             if (!(f2 = fopen(outname(argv[argc - 1]), "w")))
  86.             {
  87.  
  88.                 printf("cannot open write file %s",
  89.                     outname(argv[argc - 1]));
  90.                 printf("\n --- in main\n");
  91.                 exit(1);
  92.             }
  93.  
  94.         printf("%s:\n", argv[argc - 1]);
  95.         assemble(f1,f2);    /* call assembler */
  96.  
  97.         if (!flag)    /* close file */
  98.             fclose(f1);
  99.  
  100.         fclose(f2);
  101.     }
  102. }
  103.  
  104. /* debugging version of assemble */
  105. /* commented out because this module is now fully debugged */
  106. /* --- Na Choon Piaw, 11/14 */
  107. /*
  108. assemble(infile,outfile)
  109. FILE    *infile, *outfile;
  110. {
  111.     int    c;
  112.  
  113.     while ((c = fgetc(infile)) != EOF)
  114.         fputc(c, outfile);
  115. }
  116. */
  117.